package sortStudy;

import java.util.ArrayList;
import java.util.PriorityQueue;

public class GetLeastNumbers {

    /**
     *JZ29 最小的K个数
     * @param args
     */
    public static void main(String[] args) {

    }

    public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) {
        PriorityQueue<Integer> queue = new PriorityQueue<>(((o1, o2) -> o2 - o1));
        ArrayList<Integer> res = new ArrayList<>();
        for (int i = 0; i < k; i++){
            queue.offer(input[i]);
        }
        for (int i = k; i < input.length; i++){
            if (queue.peek() > input[i]){
                queue.poll();
                queue.offer(input[i]);
            }
        }
        while (!queue.isEmpty()){
            res.add(queue.poll());
        }
        return res;
    }
}
